﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmUser : XtraForm

    {
        public FrmUser()
        {
            InitializeComponent();
            InitGrid();
        }

        
        private void FrmUser_Load(object sender, EventArgs e)
        {
        }
        private void FrmUser_Shown(object sender, EventArgs e)
        {
            LoadData();
        }

       
        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            var userBll = new DMZS.BLL.t_user();
            try
            {
                gdcUser.DataSource = userBll.GetListForUIExpand(string.Empty).Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #region 工具栏按钮事件

        //新增
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frmNew = new FrmUserEditor();
            frmNew.ShowDialog();
        }

        //修改
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gdvUser.DataSource == null || gdvUser.RowCount == 0)
            {
                return;
            }
            var frm = new FrmUserEditor();
            frm.t_userid = Convert.ToInt32(gdvUser.GetFocusedDataRow()["t_userid"]);
            frm.ShowDialog();
        }

        //删除
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gdvUser.DataSource == null || gdvUser.RowCount == 0)
            {
                return;
            }
            if (Convert.ToInt32(gdvUser.GetFocusedDataRow()["isSystem"]) > 0)
            {
                XtraMessageBox.Show("系统项，无法删除", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }

            if (DialogResult.No == XtraMessageBox.Show("确认删除用户" + gdvUser.GetFocusedDataRow()["t_name"].ToString() + "?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            var bll = new DMZS.BLL.t_user();
            bll.Delete(Convert.ToInt32(gdvUser.GetFocusedDataRow()["t_userid"]));

            LoadData();
        }

        //刷新
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        #endregion


        #region 网格设置

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            gdvUser.IndicatorWidth = gdvUser.IndicatorWidth == -1 ? 40 : gdvUser.IndicatorWidth;
            gdvUser.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(gdvUser);
        }

        //网格数字列
        private void gdvUser_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        //网格双击修改
        private void gdvUser_DoubleClick(object sender, EventArgs e)
        {
            if (gdvUser.DataSource == null || gdvUser.RowCount == 0)
            {
                return;
            }
            var frm = new FrmUserEditor();
            frm.t_userid = Convert.ToInt32(gdvUser.GetFocusedDataRow()["t_userid"]);
            frm.ShowDialog();
        }

        //列显示内容
        private void gdvUser_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "isSystem")
            {
                if (e.Value != null && Convert.ToInt32(e.Value) > 0)
                {
                    e.DisplayText = "是";
                }
                else
                {
                    e.DisplayText = "否";
                }
            }
        }

        #endregion

    }
}
